class MyPage extends BasePopupEdit {
    constructor(dataId, pageStatus) {
        super(dataId, pageStatus);
        this._rows = 0;
    }
    OnPageLoad() {
        this.FetchForPage();
    }
    // 得到检索条件
    GetFilter() {
        let data = new Object();
        return $.toJSON(data);
    }
    //《按钮》显示全部
    FetchAll() {
        this.FetchForPage();
    }
    // 得到数据列表信息
    FetchForPage() {
        $.ajax({
            type: "POST",
            url: parent.basePath + "/trade/delivery_command_sheet/lines/fetchStyleNoOrder.html",
            timeout: 30 * 1000,
            data: JSON.stringify(parent.$("#HeaderId").val()),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done((data) => {
            this.BuildDataTable(data);
        }).fail((data) => {
            $("#loading").hide();
            ajaxError(data, 1);
            $(".error-msg").css({left: '120px', top: '3px'});
        }).always(( ) => {
        });
    }
    BuildDataTable(result) {
        $("#tabData01 tbody").empty();

        if (result === null) {
            $('#loading').hide();
            return false;
        }
        this._rows = result.length;
        let content_01 = '';
        for (let i = 0; i < result.length; i++) {
            let id = result[i].linesId;
            content_01 += '<tr _mash_linesid="' + id + '">';
            content_01 += '<td style="text-align:center;">' + result[i].orderno + '</td>';
            content_01 += '<td>' + result[i].styleno + '</td>';
            content_01 += '</tr>';

            $("#tabData01 tbody").append(content_01);
            content_01 = '';
        }

        setOneTableRowColor("tabData01");

        this.AutoAdjustTableHeightForLines();    // 自适应高度设置
        $("#loading").hide();

        let _this = this;
        // 拖动表格行改变显示顺序
        $("#tabData01 tbody").wxdragsort({
            dragWrapper: $("#tabData01"),
            placeHolderWrapper: $("#divD1"),
            parentobj: $("#tabData01"),
            dragColumnIndex: 0,
            dragEnd: function () {
                _this.ResetRowNo();
            }
        });
    }
    // 拖动改变序号
    ResetRowNo() {
        // 设置拖动后的每行序号
        $("#tabData01 tbody").find("tr").each(function (index) {
            $(this).find("td").eq(0).text(index + 1);
        });

        let _result = [];
        $('#tabData01 tbody').find("tr").each(function () {
            let _orderNo = $.trim($(this).find('td').eq(0).text());   // 顺序号
            let _styleNo = $.trim($(this).find('td').eq(1).text());   // 款号

            let item = {};
            item['HeaderId'] = parseInt(parent.$("#HeaderId").val());
            item['StyleNo'] = _styleNo;
            item['OrderNo'] = parseInt(_orderNo);
            item['UpdatedBy'] = parseInt(parent.$("#UserId").val());
            _result.push(item);
        });

        // 更新后台对应的序号
        $.ajax({
            type: "POST",
            url: parent.basePath + "/trade/delivery_command_sheet/lines/updateStyleNoOrder.html",
            timeout: 30 * 1000,
            data: JSON.stringify(_result),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done((data) => {
            // 显示明细数据
           parent._InstancePageLines.BuildDataTable(data.LinesList);

            ShowSuccessMessagePanel('款号顺序改变成功。');
            $(".success-msg").css({left: '120px', top: '3px'});
        }).fail((data) => {
            ajaxError(data, 1);
        }).always(( ) => {
        });
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _top = $("#tabHeader01").offset().top + 25;
        let _rc = Math.floor((h - _top - 25) / 25);
        let _dh = _rc * 25;
        let _tah = this._rows * 25;
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#divD1").css({"max-height": _dh, height: _dh});
    }
}

let  _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#form_data").data("changed", false);   // 初始化form数据提交状态
    _Instance = new MyPage(0, 1);
    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}